<?php
namespace app\user\controller;

use app\common\controller\UserBase;
use app\common\model\Dict as DictModel;
use app\user\model\DictData;
use app\user\model\UserGroup;
use app\user\model\UserRule;

class Dict extends UserBase
{
    protected $noAuth = ['get_dictionary'];
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        return $this->fetch('index');
    }
    public function index_json($limit='15',$dict_id)
    {
        $dict = new DictData();
        if($dict_id){
            $dict = $dict->where('dict_id',$dict_id);
        }
        $list = $dict->order('id desc')->paginate($limit);
        $this->result($list);
    }
    public function get_dictionary()
    {
        $dict = new DictModel();
        //判断权限-获取我的权限
        $auth_id = UserRule::my_auth();
        $dict = $dict->where('auth_id','in',$auth_id);
        $list = $dict->order('auth_id asc,id desc')->select();
        $data = [];
        foreach ($list as $k=>$r){
            $data[$k]['leftid'] = $k;
            $data[$k]['dictId'] = $r['id'];
            $data[$k]['dictName'] = $r['name'];
        }
        return json(['code'=>'0','msg'=>'','count'=>count($list),'data'=>$data]);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'dictData');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $result = DictData::create($param);
            if ($result == true) {
                insert_user_log('添加了字典');
                $this->success('添加成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save');
    }

    public function edit($id)
    {
        if ($this->request->isPost()) {
            $param  = $this->request->param();
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'dictData');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            //更新数据
            $resule = DictData::update($param,['id'=>$param['id']]);
            if ( $resule == true) {
                insert_user_log('修改了字典名称');
                $this->success('修改成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', [
            'data'  => DictData::where('id',$id)->find(),
        ]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            DictData::destroy($param['id']);
            insert_user_log('删除了字典名称');
            $this->success('删除成功');
            
        }
    }

}
